<?php if (SET != 1) die(); ?>

	<h1>Statistiken</h1>
	
	<p class="info">
	
		Folgende Statistik zeigt die 100 am meisten besuchten Domains des jeweiligen Monats.<br />
		In der Spalte "URL Beispiel" sehen Sie die aufgerufene URL beim ersten Erfassen der Domain.
		
	</p>
	
	<script type="text/javascript">
	
		function selectmonth(el) {
			
			var month = el.options[el.selectedIndex].value;

			window.location.href = "index.php?m=stats&f=stats&month="+month;
			
		}
	
	</script>
	
	<?php
	
		$db = new SQLiteDatabase(config("root").'data/domains');
		
		if (isset($_GET['month']) AND $_GET['month'] != "") {
			
			$month = preg_replace('@[^0-9\.]@', '', $_GET['month']);
			
		} else {
			$month = date("m.Y");
		}
		
		
	
		$months = $db->arrayQuery("SELECT month FROM domains GROUP BY month");
		
		if ($months) {
			
			echo '<strong>Monat:</strong> <select name="monthselect" size="1" onchange="selectmonth(this)">';
			
				function cmp($a, $b)
				{
					/* sort by month/year */
				    
					if ($a == $b) {
				        return 0;
				    }
				
					$aExplode = explode(".", $a['month']);
					$aTS = mktime(0, 0, 0, $aExplode[0], 15, $aExplode[1]);

					$bExplode = explode(".", $b['month']);
					$bTS = mktime(0, 0, 0, $bExplode[0], 15, $bExplode[1]);
				    
					if ($bTS > $aTS) {
						return 1;
					} else {
						return -1;
					}
				
				}

				usort($months, "cmp");

				foreach ($months as $thisMonth) {
					
					echo '<option value="'.$thisMonth['month'].'"'.(($thisMonth['month'] == $month)?" selected":"").'>'.$thisMonth['month'].'</option>';
					
				}
			
			echo '</select>';

		}
	
	?>
	
	
	<table>

		<tr>
			<th>Platz</th>
			<th>Anz. Zugriffe</th>
			<th>Domain</th>
			<th>URL Beispiel</th>
		</tr>

		<?php
		
			$topDomains = $db->arrayQuery("SELECT * FROM domains WHERE month = '".$month."' ORDER BY counter DESC LIMIT 100");
			
			if ($topDomains) {
			
				$i = 1;
			
				foreach ($topDomains as $domain) {
					
					echo '
						<tr>
							<td>'.$i.'</td>
							<td>'.$domain['counter'].'</td>
							<td><a href="http://'.$domain['domain'].'" target="_blank">'.$domain['domain'].'</a></td>
							<td><a href="'.$domain['url'].'" target="_blank">';
						
						if (strlen($domain['url']) > 60) {
							echo substr($domain['url'], 0, 60)."...";
						} else {
							echo $domain['url'];
						}
							
					echo '</a></td>
						</tr>
					';
				
					$i++;
					
				}
				
			} else {
				echo '<p class="error">Es wurden keine Domains gefunden!</p>';
			}

		?>